/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.java.controller;

//import com.java.editor.LoaiBiaEditor;
//import com.java.editor.LoaiSachEditor;
//import com.java.editor.NxbEditor;
//import com.java.editor.TacGiaEditor;
//import com.java.editor.TheLoai1Editor;
//import com.java.editor.TheLoai2Editor;
//import com.java.editor.TheLoai3Editor;
//import com.java.mapbean.Loaibia;
//import com.java.mapbean.Loaisach;
//import com.java.mapbean.Nhaxuatban;
//import com.java.mapbean.Tacgia;
//import com.java.mapbean.Theloai1;
//import com.java.mapbean.Theloai2;
//import com.java.mapbean.Theloai3;
import com.java.mapbean.Nguoidung;
import com.java.service.ChiTietHDService;
import com.java.service.SachService;
import com.java.service.TheLoai1Service;
import com.java.service.TheLoai3Service;
import com.java.service.Theloai2Service;
import com.java.service.TinhService;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.multipart.support.StringMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author quannq
 */
@Controller
public class SachController {

    private SachService sachService = new SachService();
    private TheLoai1Service tl1Service = new TheLoai1Service();
    private TinhService tinhService = new TinhService();
    private ChiTietHDService cthdService = new ChiTietHDService();
    private Theloai2Service tl2Service = new Theloai2Service();
    private TheLoai3Service tl3Service = new TheLoai3Service();

    protected void initBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor editor = new CustomDateEditor(df, true);
        binder.registerCustomEditor(Date.class, editor);
//        binder.registerCustomEditor(Theloai1.class, new TheLoai1Editor(this.theLoai1Service));
//        binder.registerCustomEditor(Theloai2.class, new TheLoai2Editor(this.theLoai2Service));
//        binder.registerCustomEditor(Theloai3.class, new TheLoai3Editor(this.theLoai3Service));
//        binder.registerCustomEditor(Tacgia.class, new TacGiaEditor(this.tacgiaService));
//        binder.registerCustomEditor(Loaibia.class, new LoaiBiaEditor(this.loaiBiaService));
//        binder.registerCustomEditor(Loaisach.class, new LoaiSachEditor(this.loaiSachService));
//        binder.registerCustomEditor(Nhaxuatban.class, new NxbEditor(this.nhaXuatBanService));
//        binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
        request.setCharacterEncoding("UTF-8");
    }

    public void init(ModelMap model) {
        model.addAttribute("top5", cthdService.top5(1));
        model.addAttribute("TheLoai1", tl1Service.getAll());
        model.addAttribute("Tinh", tinhService.getAll());
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String sach(ModelMap model) {
        init(model);
        model.addAttribute("SachKM", sachService.khuyenMai4(1));
        model.addAttribute("numPageKM", sachService.pageKM(4));
        model.addAttribute("SachKhac", sachService.khac4(1));
        model.addAttribute("numPageKhac", sachService.pageKhac(4));
        model.addAttribute("contentpage", "index.jsp");
        return "index";
    }

    @RequestMapping(value = "/get4KM/{page}", method = RequestMethod.GET)
    public ModelAndView get4KM(@PathVariable(value = "page") int page, ModelMap model) {
        // System.out.println(page);
        model.addAttribute("ListSach", sachService.khuyenMai4(page));
        return new ModelAndView("ajax/sach4");
    }

    @RequestMapping(value = "/get4Khac/{page}", method = RequestMethod.GET)
    public ModelAndView get4Khac(@PathVariable(value = "page") int page, ModelMap model) {
        // System.out.println(page);
        model.addAttribute("ListSach", sachService.khac4(page));
        return new ModelAndView("ajax/sach4");
    }

    @RequestMapping(value = "/get4KM/{id1}/{page}", method = RequestMethod.GET)
    public ModelAndView get4KMTl1(@PathVariable(value = "page") int page,
            @PathVariable(value = "id1") int id1,
            ModelMap model) {
        // System.out.println(page);
        model.addAttribute("ListSach", sachService.khuyenMai4(id1, page));
        return new ModelAndView("ajax/sach4");
    }

    @RequestMapping(value = "/get4Khac/{id1}/{page}", method = RequestMethod.GET)
    public ModelAndView get4KhacTl1(@PathVariable(value = "page") int page,
            @PathVariable(value = "id1") int id1,
            ModelMap model) {
        // System.out.println(page);
        model.addAttribute("ListSach", sachService.khac4(id1, page));
        return new ModelAndView("ajax/sach4");
    }

    @RequestMapping(value = "/get4KM/{id1}/{id2}/{page}", method = RequestMethod.GET)
    public ModelAndView get4KMTl2(@PathVariable(value = "page") int page,
            @PathVariable(value = "id1") int id1,
            @PathVariable(value = "id2") int id2,
            ModelMap model) {
        // System.out.println(page);
        model.addAttribute("ListSach", sachService.khuyenMai4(id1, id2, page));
        return new ModelAndView("ajax/sach4");
    }

    @RequestMapping(value = "/get4Khac/{id1}/{id2}/{page}", method = RequestMethod.GET)
    public ModelAndView get4KhacTl2(@PathVariable(value = "page") int page,
            @PathVariable(value = "id1") int id1,
            @PathVariable(value = "id2") int id2,
            ModelMap model) {
        // System.out.println(page);
        model.addAttribute("ListSach", sachService.khac4(id1, id2, page));
        return new ModelAndView("ajax/sach4");
    }

    @RequestMapping(value = "/getSachId/{id}", method = RequestMethod.GET)
    public ModelAndView getSachId(@PathVariable(value = "id") int id, ModelMap model) {
        model.addAttribute("Sach", sachService.findId(id));
        return new ModelAndView("ajax/sachInfo");
    }

    @RequestMapping(value = "/view-all-khuyen-mai/page/{page}", method = RequestMethod.GET)
    public String viewAllKM(@PathVariable(value = "page") int page, ModelMap model) {
        init(model);
        model.addAttribute("Sach", sachService.khuyenMai10(page));
        model.addAttribute("numPage", sachService.pageKM(10));
        model.addAttribute("numSach", sachService.countSachKM());
        model.addAttribute("curPage", page);
        model.addAttribute("loaiSach", "KM");
        model.addAttribute("contentpage", "viewAll.jsp");
        return "index";
    }

    @RequestMapping(value = "/view-all-khac/page/{page}", method = RequestMethod.GET)
    public String viewAllKhac(@PathVariable(value = "page") int page, ModelMap model) {
        init(model);
        model.addAttribute("Sach", sachService.khac10(page));
        model.addAttribute("numPage", sachService.pageKhac(10));
        model.addAttribute("numSach", sachService.countSachKhac());
        model.addAttribute("curPage", page);
        model.addAttribute("loaiSach", "Khac");
        model.addAttribute("contentpage", "viewAll.jsp");
        return "index";
    }

    @RequestMapping(value = "/view-all-khuyen-mai/{id1}/page/{page}", method = RequestMethod.GET)
    public String viewAllKMTl1(@PathVariable(value = "page") int page,
            @PathVariable(value = "id1") int id1,
            ModelMap model) {
        init(model);
        model.addAttribute("Sach", sachService.khuyenMai10(id1, page));
        model.addAttribute("numPage", sachService.pageKM(id1, 10));
        model.addAttribute("numSach", sachService.countSachKM(id1));
        model.addAttribute("curPage", page);
        model.addAttribute("id1", id1);
        model.addAttribute("loaiSach", "KMTl1");
        model.addAttribute("contentpage", "viewAll.jsp");
        return "index";
    }

    @RequestMapping(value = "/view-all-khuyen-mai/{id1}/{id2}/page/{page}", method = RequestMethod.GET)
    public String viewAllKMTl2(@PathVariable(value = "page") int page,
            @PathVariable(value = "id1") int id1,
            @PathVariable(value = "id2") int id2,
            ModelMap model) {
        init(model);
        model.addAttribute("Sach", sachService.khuyenMai10(id1, id2, page));
        model.addAttribute("numPage", sachService.pageKM(id1, id2, 10));
        model.addAttribute("numSach", sachService.countSachKM(id1, id2));
        model.addAttribute("curPage", page);
        model.addAttribute("id1", id1);
        model.addAttribute("id2", id2);
        model.addAttribute("loaiSach", "KMTl2");
        model.addAttribute("contentpage", "viewAll.jsp");
        return "index";
    }

    @RequestMapping(value = "/view-all-khac/{id1}/page/{page}", method = RequestMethod.GET)
    public String viewAllKhacTl1(@PathVariable(value = "page") int page,
            @PathVariable(value = "id1") int id1,
            ModelMap model) {
        init(model);
        model.addAttribute("Sach", sachService.khac10(id1, page));
        model.addAttribute("numPage", sachService.pageKhac(id1, 10));
        model.addAttribute("numSach", sachService.countSachKhac(id1));
        model.addAttribute("curPage", page);
        model.addAttribute("id1", id1);
        model.addAttribute("loaiSach", "KhacTl1");
        model.addAttribute("contentpage", "viewAll.jsp");
        return "index";
    }

    @RequestMapping(value = "/view-all-khac/{id1}/{id2}/page/{page}", method = RequestMethod.GET)
    public String viewAllKhacTl2(@PathVariable(value = "page") int page,
            @PathVariable(value = "id1") int id1,
            @PathVariable(value = "id2") int id2,
            ModelMap model) {
        init(model);
        model.addAttribute("Sach", sachService.khac10(id1, id2, page));
        model.addAttribute("numPage", sachService.pageKhac(id1, id2, 10));
        model.addAttribute("numSach", sachService.countSachKhac(id1, id2));
        model.addAttribute("curPage", page);
        model.addAttribute("id1", id1);
        model.addAttribute("id2", id2);
        model.addAttribute("loaiSach", "KhacTl2");
        model.addAttribute("contentpage", "viewAll.jsp");
        return "index";
    }

    @RequestMapping(value = "/view-sach/{id}", method = RequestMethod.GET)
    public String viewSach(@PathVariable(value = "id") int id, ModelMap model) {
        init(model);
        model.addAttribute("SachId", sachService.findId(id));
        model.addAttribute("contentpage", "sachChiTiet.jsp");
        return "sachChiTiet";
    }

    @RequestMapping(value = "/sach-the-loai/{id1}", method = RequestMethod.GET)
    public String viewSachTheLoai1(@PathVariable(value = "id1") int id1, ModelMap model) {
        init(model);
        model.addAttribute("TheLoai1Id", tl1Service.findId(id1));
        model.addAttribute("SachKM", sachService.khuyenMai4(id1, 1));
        model.addAttribute("numPageKM", sachService.pageKM(id1, 4));
        model.addAttribute("SachKhac", sachService.khac4(id1, 1));
        model.addAttribute("numPageKhac", sachService.pageKhac(id1, 4));
        model.addAttribute("contentpage", "sachTheLoai1.jsp");

        return "sachTheLoai";
    }

    @RequestMapping(value = "/sach-the-loai/{id1}/{id2}", method = RequestMethod.GET)
    public String viewSachTheLoai2(@PathVariable(value = "id1") int id1,
            @PathVariable(value = "id2") int id2,
            ModelMap model) {
        init(model);
        model.addAttribute("TheLoai1Id", tl1Service.findId(id1));
        model.addAttribute("SachKM", sachService.khuyenMai4(id1, id2, 1));
        model.addAttribute("numPageKM", sachService.pageKM(id1, id2, 4));
        model.addAttribute("SachKhac", sachService.khac4(id1, id2, 1));
        model.addAttribute("numPageKhac", sachService.pageKhac(id1, id2, 4));
        model.addAttribute("TheLoai2Id", tl2Service.findId(id2));
        model.addAttribute("contentpage", "sachTheLoai2.jsp");
        return "sachTheLoai";
    }

    @RequestMapping(value = "/sach-the-loai/{id1}/{id2}/{id3}/page/{page}", method = RequestMethod.GET)
    public String viewSachTheLoai3(@PathVariable(value = "id1") int id1,
            @PathVariable(value = "id2") int id2,
            @PathVariable(value = "id3") int id3,
            @PathVariable(value = "page") int page,
            ModelMap model) {
        init(model);
        model.addAttribute("TheLoai1Id", tl1Service.findId(id1));
        model.addAttribute("id2", id2);
        model.addAttribute("TheLoai3Id", tl3Service.findId(id3));
        model.addAttribute("Sach", sachService.getAll(id1, id2, id3, page));
        model.addAttribute("numPage", sachService.pageAll(id1, id2, id3));
        model.addAttribute("numSach", sachService.countAll(id1, id2, id3));
        model.addAttribute("curPage", page);
        model.addAttribute("loaiSach", "All3");
        model.addAttribute("contentpage", "viewAll.jsp");
        return "sachTheLoai";
    }

    @RequestMapping(value = "/timkiem", method = RequestMethod.GET)
    public String searchSach(@RequestParam("hint_q") String hint_q,
            @RequestParam("cid") int cid, @RequestParam("page") int page,
            ModelMap model) {
        init(model);
        System.out.println(hint_q);
        model.addAttribute("Sach", sachService.searchSach(cid, hint_q, page));
        model.addAttribute("numPage", sachService.pageSearch(cid, hint_q, 10));
        model.addAttribute("numSach", sachService.countSachSearch(cid, hint_q));
        model.addAttribute("curPage", page);
        model.addAttribute("noidung", hint_q);
        model.addAttribute("cid", cid);
        model.addAttribute("loaiSach", "Search");
        model.addAttribute("contentpage", "viewAll.jsp");
        return "index";
    }
}
